import io

from PIL import Image


def combine_images(image_blobs, direction='horizontal'):
    # 加载图片
    images = [Image.open(io.BytesIO(blob)) for blob in image_blobs]

    # 获取单个图片的宽度和高度
    widths, heights = zip(*(i.size for i in images))

    if direction == 'horizontal':
        # 横向拼接的总宽度为所有图片宽度之和，高度取最高的
        total_width = sum(widths)
        max_height = max(heights)
        new_img = Image.new('RGB', (total_width, max_height))

        # 按顺序粘贴每张图片
        x_offset = 0
        for img in images:
            new_img.paste(img, (x_offset, 0))
            x_offset += img.width
        new_img.save('combined_image.jpg')

    elif direction == 'vertical':
        # 纵向拼接的总高度为所有图片高度之和，宽度取最宽的
        total_height = sum(heights)
        max_width = max(widths)
        new_img = Image.new('RGB', (max_width, total_height))

        # 按顺序粘贴每张图片
        y_offset = 0
        for img in images:
            new_img.paste(img, (0, y_offset))
            y_offset += img.height
        new_img.save('combined_image.jpg')


def wmf_to_png(blob, b):
    with Image.open(io.BytesIO(blob)) as img:
        img.save(b, 'PNG')
